/*
 * Copyright 2011-2011 Tiago da Fonseca Frazao  All rights reserved.
 */
package mz.co.research.server.mvc.services;

import java.util.Collection;

import mz.co.framework.context.IUserContext;
import mz.co.framework.error.AbstractException;
import mz.co.framework.error.SystemException;
import mz.co.research.server.mvc.model.dao.jpa.AnswerTypeJpaDao;
import mz.co.research.shared.model.AnswerType;
import mz.co.research.shared.services.IAnswerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AnswerService extends AbstractService<AnswerService> implements
		IAnswerService {

	@Autowired
	private AnswerTypeJpaDao answerTypeJpaDao;

	@Transactional
	public void createAnswerType(IUserContext context, AnswerType answerType)
			throws AbstractException {
		answerTypeJpaDao.persist(answerType);
	}

	@Transactional
	public void activeAnswerType(IUserContext context, AnswerType answerType,
			boolean active) throws AbstractException {
		answerType.setActive(active);
		answerTypeJpaDao.merge(answerType);
	}

	@Transactional
	public AnswerType updateAnswerType(IUserContext context,
			AnswerType answerType) throws AbstractException {
		return answerTypeJpaDao.merge(answerType);
	}

	public AnswerType findAnswerTypeById(IUserContext context, Long id)
			throws SystemException {
		return answerTypeJpaDao.findById(id);
	}

	public AnswerType findAnswerTypeByType(IUserContext context, String type)
			throws SystemException {

		return answerTypeJpaDao.findByType(type);
	}

	public Collection<AnswerType> findAnswerTypeByActive(boolean active)
			throws SystemException {

		return answerTypeJpaDao.findByActive(active);
	}

	public Collection<AnswerType> findAnswerTypes() throws SystemException {
		return answerTypeJpaDao.findByActive(Boolean.TRUE);
	}

	public AnswerType findAnswerType(String type) throws SystemException {
		return answerTypeJpaDao.findByType(type);
	}
}
